package com.whf.android.jar.util.number;

import android.graphics.Color;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.ObjectUtils;

/**
 * 红涨绿跌
 *
 * @author qf
 * @version 1.0
 */
public final class RiseFallUtils {

    @NonNull
    public static String rate(Object obj) {
        double zb = objToDouble(obj);
        if (zb == 0) {
            return "一";
        }
        return zb + "%";
    }

    /**
     * @param obj:红涨绿跌（国内）
     */
    public static int riseFallColor(Object obj) {
        double zb = objToDouble(obj);
        final String dian = zb > 0 ? "#FF4F2C" : "#36D084";
        return Color.parseColor(dian);
    }

    @NonNull
    public static String riseFall(Object obj) {
        double zb = objToDouble(obj);
        if (zb == 0) {
            return "一";
        }
        return (zb > 0 ? "+" : "") + zb;
    }

    @NonNull
    public static String riseFallRate(Object obj) {
        double zb = objToDouble(obj);
        if (zb == 0) {
            return "一";
        }
        return (zb > 0 ? "+" : "") + zb + "%";
    }

    @NonNull
    public static String riseFallInt(Object obj) {
        int zb = (int) objToDouble(obj);
        if (zb == 0) {
            return "一";
        }
        return (zb > 0 ? "+" : "") + zb;
    }

    @NonNull
    public static String riseFallIntRate(Object obj) {
        int zb = (int) objToDouble(obj);
        if (zb == 0) {
            return "一";
        }
        return (zb > 0 ? "+" : "") + zb + "%";
    }

    /**
     * @param obj:红涨绿跌（国内）
     */
    public static boolean riseFallIs(Object obj) {
        double zb = objToDouble(obj);
        return zb > 0;
    }

    /**
     * @param obj：Object转换为double
     */
    public static double objToDouble(Object obj) {
        if (ObjectUtils.isEmpty(obj) || "null".equals(obj) || isFloat(obj)) {
            obj = "0";
        }
        return Double.parseDouble(String.valueOf(obj));
    }


    /**
     * 判断字符串是数字
     */
    public static boolean isFloat(Object value) {
        try {
            Double.parseDouble(String.valueOf(value));
            return false;
        } catch (NumberFormatException e) {
            return true;
        }
    }
}
